package com.app.micaihu.view.login.activity

import android.os.Bundle
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.UserInfor
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.view.login.contract.LogInContract
import com.app.micaihu.view.login.dialog.ChangeMilitaryDialog
import com.app.micaihu.view.login.presenter.LogInPresenter
import com.app.micaihu.view.login.sy.ConfigUtils
import com.app.micaihu.view.login.utils.CloseLoginActivityUtils
import com.app.utils.biz.Biz.Companion.instance
import com.blankj.utilcode.util.*
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.login_first_activity.*


/**
 * Created by Guo on 2016/6/15.
 * 快捷登录
 */
class QuickLoginActivity : BaseMvpActivity<LogInContract.View, LogInPresenter>(), LogInContract.View {
    override fun getPresenter() = LogInPresenter()
    override fun getLayoutId() = R.layout.login_first_activity
    var bundle = Bundle()
    override fun getBundle(bundle: Bundle) {
        super.getBundle(bundle)
        this.bundle = bundle
    }

    private var forceLogin = false

    override fun setData() {
        forceLogin = SPUtils.getInstance().getBoolean(ConfigKeyConstant.forceLogin)

        OneKeyLoginManager.getInstance().setAuthThemeConfig(ConfigUtils.getCJSConfig(thisActivity, forceLogin), null)
        val lastUserInfo = instance.lastUserInfo
        if (StringUtils.isEmpty(lastUserInfo)) {
            openLoginAuth()
        } else {
            val userInfo = GsonUtils.fromJson(lastUserInfo, UserInfor::class.java)

            if (userInfo != null) {
                enterPager(LastLoginActivity::class.java, this.bundle)
            } else {
                enterPager(PhoneLoginActivity::class.java, this.bundle)
            }
            finish()
        }
    }

    override fun addListeners() {

    }

    /**
     * 拉取授权页方法
     */
    private fun openLoginAuth() {
        OneKeyLoginManager.getInstance().openLoginAuth(false, { code, result ->
            if (1000 == code) {
//                    isOpenAuth = true;
                //拉起授权页成功
                LogUtils.e("VVV", "拉起授权页成功： _code==$code   _result==$result")

                if (forceLogin) {
                    ViewUtils.runOnUiThreadDelayed({
                        val changeMilitaryDialog = ChangeMilitaryDialog(this)
                        changeMilitaryDialog.setChangeMilitaryListener(object : ChangeMilitaryDialog.OnChangeMilitaryListener {
                            override fun changeMilitary(index: Int) {
                                ConfigUtils.tvTitle?.text = "欢迎来到迷彩虎" + Config.militaryList[index - 1]
                            }
                        })
                        changeMilitaryDialog.show()
                    }, 200)
                }
            } else {
                LogUtils.e("VVV", "拉起授权页失败： _code==$code   _result==$result")
                OneKeyLoginManager.getInstance().finishAuthActivity()
                OneKeyLoginManager.getInstance().removeAllListener()
                enterPager(PhoneLoginActivity::class.java, bundle)
                progressBar.postDelayed({ finish() }, 200)
            }
        }) { code, result ->
            if (1011 == code) {
//                    isOpenAuth = false;
                LogUtils.e("VVV", "用户点击授权页返回： _code==$code   _result==$result")
                if (forceLogin) {
                    OneKeyLoginManager.getInstance().finishAuthActivity()
                    OneKeyLoginManager.getInstance().removeAllListener()
                    ActivityUtils.finishAllActivities()
                } else {
                    finish()
                }
            } else if (1000 == code) {
                LogUtils.e("VVV", "用户点击登录获取token成功： _code==$code   _result==$result")
                val token = JsonUtils.getString(result, "token")
                LogUtils.e("token->$token")
                if (StringUtils.isEmpty(token)) {
                    enterPager(PhoneLoginActivity::class.java, bundle)
                } else {
                    mPresenter?.flashLogin(token)
                }
            } else {
                LogUtils.e("VVV", "用户点击登录获取token失败： _code==$code   _result==$result")
                enterPager(PhoneLoginActivity::class.java, bundle)
                progressBar.postDelayed({ finish() }, 200)
            }
            OneKeyLoginManager.getInstance().finishAuthActivity()
            OneKeyLoginManager.getInstance().removeAllListener()
        }
    }

    override fun handleFlashLogin(login: BaseBean<UserInfor?>?) {
        if (login == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (login.isSuccess()) {
                login.data?.let {
                    //设置用户信息
                    UserInforManager.getInstance().userInfor = it//设置用户信息
                    //保存到本地
                    UserInforManager.getInstance().saveUserInfor(it)
                    //拉取任务数据
                    if (forceLogin) {
                        UserInforManager.getInstance().getUserTask(this, false)
                    } else {
                        UserInforManager.getInstance().getUserTask(this, true)
                    }
                    LiveEventBus.get(Config.EventBus.loginSuccess).post(true)
                    if (it.mobile.isEmpty()) {
                        val bundle = Bundle()
                        bundle.putBoolean(Config.Extra.showSkip, true)
                        enterPager(BindPhoneActivity::class.java, bundle)
                    }
                    CloseLoginActivityUtils.closeAllLoginActivity()
                }
            } else {
                ToastUtils.showShort(login.msg)
            }
        }
    }

    override fun onBackPressed() {
        if (forceLogin) {
            OneKeyLoginManager.getInstance().finishAuthActivity()
            OneKeyLoginManager.getInstance().removeAllListener()
            ActivityUtils.finishAllActivities()
        } else {
            super.onBackPressed()
        }


    }
}